const { Service } = require('egg');

class AdminService extends Service {
  // 获取城市数据
  async getCityList() {
    try {
      // console.log('经历了服务层');
      const cityList = await this.app.model.City.findAll({
        include: [
          {
            model: this.app.model.Hotel,
          },
        ],
      });
      // 判断有没有数据
      if (cityList) {
        this.ctx.body = {
          code: 200,
          data: cityList,
        };
      } else {
        this.ctx.body = {
          code: 400,
          data: '数据获取失败，请联系管理员',
        };
      }
      return cityList;
    } catch (error) {
      // 获取失败
      return error;
    }
  }
  // 根据城市id查询所有酒店信息
  async getListByCityid(id) {
    try {
      // console.log('经历了服务层');
      const hotelList = await this.app.model.City.findOne({
        where: {
          id,
        },
        include: [{
          model: this.app.model.Hotel,
        }],
      }
      );
      // 判断有没有数据
      if (hotelList) {
        this.ctx.body = {
          code: 200,
          data: hotelList,
        };
      } else {
        this.ctx.body = {
          code: 400,
          data: '数据获取失败，请联系管理员',
        };
      }
      return hotelList;
    } catch (error) {
      // 获取失败
      return error;
    }
  }
}
module.exports = AdminService;
